package com.hnluchuan.staff.service;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.staff.dao.EmployeeFileDAO;
import com.hnluchuan.staff.dto.EmployeeFileDTO;
import com.hnluchuan.staff.model.EmployeeFile;
import com.hnluchuan.utils.common.BeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class EmployeeFileService extends BaseService {

    @Autowired
    private EmployeeFileDAO employeeFileDAO;
    public List<EmployeeFileDTO> find(EmployeeFileDTO query, Page page) {
        List<EmployeeFile> models = employeeFileDAO.find(query, page);
        List<EmployeeFileDTO> dtos = toDTOs(models);
        return dtos;
    }

    private List<EmployeeFileDTO> toDTOs(List<EmployeeFile> models) {
        if (CollectionUtils.isEmpty(models)) {
            return new ArrayList<EmployeeFileDTO>(0);
        }
        List<EmployeeFileDTO> dtos = new ArrayList<EmployeeFileDTO>(models.size());
        for (EmployeeFile model : models) {
            EmployeeFileDTO dto = toDTO(model);
            dtos.add(dto);
        }
        return dtos;
    }

    public Long create(EmployeeFileDTO dto) {
        EmployeeFile model = new EmployeeFile();
        toModel(model, dto);
        return employeeFileDAO.create(model);
    }

    public EmployeeFileDTO load(Long id) {
        EmployeeFile model = employeeFileDAO.load(id);
        return toDTO(model);
    }

    public void updateAllFields(EmployeeFileDTO dto) {
        EmployeeFile model = employeeFileDAO.load(dto.getId());
        toModel(model, dto);
        employeeFileDAO.update(model);
    }

    private EmployeeFileDTO toDTO(EmployeeFile model) {
        if (model == null) {
            return null;
        }
        EmployeeFileDTO dto = BeanMapper.map(model, EmployeeFileDTO.class);
        return dto;
    }

    private void toModel(EmployeeFile model, EmployeeFileDTO dto) {
        BeanMapper.copy(dto, model);
    }

    public List<EmployeeFileDTO> findAll(EmployeeFileDTO query) {
        return this.find(query, null);
    }
}
